package com.mashibing.juc.c_022_RefTypeAndThreadLocal;

import java.lang.ref.WeakReference;

/**
 * 弱引用
 * 只要找遇到gc就会回收
 * 通常用在容器里
 * @author: Yuanzt
 * @Date: 2021/3/31
 */
public class T03_WeakRefernce {
    public static void main(String[] args) {
        WeakReference<M> m = new WeakReference<>(new M());

        System.out.println(m.get());
        System.gc();
        System.out.println(m.get());

        //弱引用
        ThreadLocal<M> tl = new ThreadLocal<>();
        tl.set(new M());
        tl.remove();//用完之后必须remove，不然还会造成内存泄露
    }
}
